package cn.kgc.mes.type.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.kgc.mes.common.annotation.Log;
import cn.kgc.mes.common.core.controller.BaseController;
import cn.kgc.mes.common.core.domain.AjaxResult;
import cn.kgc.mes.common.enums.BusinessType;
import cn.kgc.mes.type.domain.UnitMeasure;
import cn.kgc.mes.type.service.IUnitMeasureService;
import cn.kgc.mes.common.utils.poi.ExcelUtil;
import cn.kgc.mes.common.core.page.TableDataInfo;

/**
 * 单位Controller
 * 
 * @author ydw
 * @date 2025-09-10
 */
@RestController
@RequestMapping("/measure/measure")
public class UnitMeasureController extends BaseController
{
    @Autowired
    private IUnitMeasureService unitMeasureService;

    /**
     * 查询单位列表
     */
//    @PreAuthorize("@ss.hasPermi('measure:measure:list')")
    @GetMapping("/list")
    public TableDataInfo list(UnitMeasure unitMeasure)
    {
        startPage();
        List<UnitMeasure> list = unitMeasureService.selectUnitMeasureList(unitMeasure);
        return getDataTable(list);
    }

    /**
     * 导出单位列表
     */
//    @PreAuthorize("@ss.hasPermi('measure:measure:export')")
    @Log(title = "单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UnitMeasure unitMeasure)
    {
        List<UnitMeasure> list = unitMeasureService.selectUnitMeasureList(unitMeasure);
        ExcelUtil<UnitMeasure> util = new ExcelUtil<UnitMeasure>(UnitMeasure.class);
        util.exportExcel(response, list, "单位数据");
    }

    /**
     * 获取单位详细信息
     */
//    @PreAuthorize("@ss.hasPermi('measure:measure:query')")
    @GetMapping(value = "/{measureId}")
    public AjaxResult getInfo(@PathVariable("measureId") Long measureId)
    {
        return success(unitMeasureService.selectUnitMeasureByMeasureId(measureId));
    }

    /**
     * 新增单位
     */
//    @PreAuthorize("@ss.hasPermi('measure:measure:add')")
    @Log(title = "单位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UnitMeasure unitMeasure)
    {
        return toAjax(unitMeasureService.insertUnitMeasure(unitMeasure));
    }

    /**
     * 修改单位
     */
//    @PreAuthorize("@ss.hasPermi('measure:measure:edit')")
    @Log(title = "单位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UnitMeasure unitMeasure)
    {
        return toAjax(unitMeasureService.updateUnitMeasure(unitMeasure));
    }

    /**
     * 删除单位
     */
//    @PreAuthorize("@ss.hasPermi('measure:measure:remove')")
    @Log(title = "单位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{measureIds}")
    public AjaxResult remove(@PathVariable Long[] measureIds)
    {
        return toAjax(unitMeasureService.deleteUnitMeasureByMeasureIds(measureIds));
    }
}
